package nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import config.Config;
import crono.Cronometro;

public class LocalFileCopyNIO {
	public static void main(String[] args) throws IOException {
		Cronometro timer = new Cronometro();
		timer.start();

		try (FileInputStream fis = new FileInputStream(new File(
				Config.INPUT_FILE));
				FileOutputStream fos = new FileOutputStream(new File(
						Config.OUTPUT_FILE_NIO));) {

			ByteBuffer buf = ByteBuffer.allocateDirect(Config.LOCAL_BUF_SIZE);

			FileChannel fisCh = fis.getChannel();
			FileChannel fosCh = fos.getChannel();

			int size = Integer.MAX_VALUE;
			while (size > 0) {
				buf.clear();
				size = fisCh.read(buf);
				buf.flip();
				if (size > 0) {
					while (buf.hasRemaining()) {
						fosCh.write(buf);
					}
				}
			}
		}

		timer.stop();
		timer.print("NIO. Copy local file");
	}
}
